package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.LawyerMapper;
import com.example.demo.pojo.Lawyer;
import com.example.demo.utils.LayuiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/end/lawyer")
public class EndLawyerController {

    @Resource
    private LawyerMapper lawyerMapper;


    @RequestMapping("/add")
    public Object add(@RequestBody Lawyer lawyer){
        lawyerMapper.insert(lawyer);
        return LayuiResult.addSuccess();
    }


    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id){
        Lawyer lawyer = new Lawyer();
        lawyer.setId(id);
        lawyer.setStatus(0);
        lawyerMapper.updateById(lawyer);
        return LayuiResult.deleteSuccess();
    }


    @RequestMapping("/update")
    public Object update(@RequestBody Lawyer lawyer){
        lawyerMapper.updateById(lawyer);
        return LayuiResult.updateSuccess();
    }

    @RequestMapping("/get")
    public Object get(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<Lawyer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<Lawyer> lawyers = lawyerMapper.selectList(queryWrapper);
        Integer count = lawyerMapper.selectCount(queryWrapper);
        PageInfo<Lawyer> pageInfo = new PageInfo<>(lawyers);
        return LayuiResult.selectSuccess(pageInfo.getList(),count);
    }

    @RequestMapping("/search/{lawyerName}")
    public Object search(@PathVariable String lawyerName, Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<Lawyer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1).like("lawyer_name",lawyerName);
        List<Lawyer> lawyers = lawyerMapper.selectList(queryWrapper);
        Integer count = lawyerMapper.selectCount(queryWrapper);
        PageInfo<Lawyer> pageInfo = new PageInfo<>(lawyers);
        return LayuiResult.searchSuccess(pageInfo.getList(),count);
    }

}
